package com.zenika.zenbinding.client.handler;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.model.ValueModel;

public class CheckBoxHandler extends ValueBindingHandler implements ClickListener {

	private CheckBox checkBox;
	private ValueModel model;

	public CheckBoxHandler(CheckBox checkBox, ValueModel model) {
		super();
		this.checkBox = checkBox;
		this.model = model;
		this.model.setHandler(this);
		this.bind();
	}

	private void bind() {
		if (this.model.getValue().equals("true")) {
			this.checkBox.setChecked(true);
		} else {
			this.checkBox.setChecked(false);
		}
		this.checkBox.addClickListener(this);
	}

	public void onClick(Widget sender) {
		String result = (this.checkBox.isChecked()) ? "true" : "false";
		this.fireChange(result, this);
	}

	public void setErrorStyle() {
		// TODO Auto-generated method stub

	}

	public void setRegularStyle() {
		// TODO Auto-generated method stub

	}

	public Widget getWidget() {
		return this.checkBox;
	}

	public Object getValue() {
		return new Boolean(this.checkBox.isChecked());
	}

}
